
import datajson from '@/data' //测试用数据

const BASE_URL = 'http://192.168.1.90:8013/index.php/';

//后端请求域名
const functions = {
	test: function(data) {
		return httpRequest('/baseinformation/Fauiltfile/datafind', 'post', data);
	},
}
//前端调用的方法
const method = {
	test: {
		url: '/baseinformation/Fauiltfile/datafind',
		type: 'GET'
	}
}

const request = (options) => {
	return new Promise((resolve, reject) => {
		const defaultHeader = {
			'Content-Type': 'application/x-www-form-urlencoded'
		};

		// 如果有token，自动添加到header
		const token = null; //uni.getStorageSync('token');
		if (token) {
			defaultHeader.Authorization = `Bearer ${token}`;
		}

		uni.request({
			url: BASE_URL + options.url,
			method: options.method || 'GET',
			data: options.data || {},
			header: Object.assign({}, defaultHeader, options.header || {}),
			success: (res) => {
				if (res.statusCode === 200) {
					resolve(res.data);
				} else {
					reject(new Error(`请求失败，状态码：${res.statusCode}`));
				}
			},
			fail: (err) => {
				reject(err);
			}
		});
	});
};

// 封装具体接口
export const httpsend = (res, params = {}) => {

	//模拟异步请求数据
	return new Promise(resolve => {
		setTimeout(() => {
			resolve(datajson[res]);
			if (datajson[res] && datajson[res]['code']) {
				if (datajson[res]['code'] == 1) {
					console.log('dd', datajson[res]);

				} else if (datajson[res]['code'] == 0) {
					console.log('失败', datajson[res]);
				}
			}

		}, 1000)
	})

	//下面是真正的请求接口	
// 	return request({
// 		url: method[res].url,
// 		method: method[res].type,
// 		data: params
// 	}).then(res => {
// 		if (res.code === 1) {
// 			return res;
// 		} else {
// 			return Promise.reject(res);
// 		}
	// });
};